#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=410;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL dis[maxn][maxn];
int main(void)
{
    cin.tie(0);std::ios::sync_with_stdio(false);
    LL n,m;cin>>n>>m;
    memset(dis,0x3f,sizeof(dis));
    for(LL i=1;i<=m;i++){
        LL u,v,w;cin>>u>>v>>w;
        dis[u][v]=min(dis[u][v],w);
    }
    LL ans=0;
    for(LL k=1;k<=n;k++){
        for(LL i=1;i<=n;i++){
            for(LL j=1;j<=n;j++){
                if(i==j||i==k||j==k) continue;
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
            }
        }
        for(LL i=1;i<=n;i++){
            for(LL j=1;j<=n;j++){
                if(dis[i][j]==0x3f3f3f3f3f3f3f3fLL) continue;
                ans+=dis[i][j];
            }
        }
    }
    cout<<ans<<"\n";
    return 0;
}
